package com.sijiao.service.Impl;

import com.sijiao.common.CreateNumberId;
import com.sijiao.dao.ReceiveDao;
import com.sijiao.pojo.ProductMangementPojo;
import com.sijiao.pojo.ResultMessage;
import com.sijiao.pojo.SpuExtPojo;
import com.sijiao.service.ReceiveDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 处理数据入库的业务层
 *
 * @author ShiLian
 * @date 2022/07/21 20:17
 **/
public class ReceiveServiceImpl implements ReceiveDataService {

    private static final Logger log = LoggerFactory.getLogger(ReceiveServiceImpl.class);

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            4,
            4,
            3,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(3),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );

    @Autowired
    private ReceiveDao receiveDao;

    /**
     * 接收数据并处理
     *
     * @param models
     * @return 返回一个结果集，告诉前端入库成功与否
     */
    @Override
    public ResultMessage ReceiveData(List<ProductMangementPojo> models) {
        ResultMessage resultMessage = new ResultMessage();

        try{
            executor.execute(()->productWarehousing(models));
            resultMessage.setMessage("the product Warehousing success!");
            resultMessage.setFlag(true);
            resultMessage.setStatus(200);
        }catch (Exception e){
            log.error("the product Warehousing failed!",e);
            resultMessage.setMessage("failed");
            resultMessage.setFlag(false);
            resultMessage.setStatus(500);
        }
        return resultMessage;
    }

    private void productWarehousing(List<ProductMangementPojo> models){
        Integer propId = CreateNumberId.getUUIDInOrderId();
        skuMethod(models);
        spuMethod(models);
    }

    /**
     * sku的操作
     *
     * @param models
     */
    private void skuMethod(List<ProductMangementPojo> models){
        for (ProductMangementPojo model : models) {
            Integer skuId = CreateNumberId.getUUIDInOrderId();
            model.getField();
        }
    }

    /**
     * spu的操作
     *
     * @param models
     */
    private void spuMethod(List<ProductMangementPojo> models){
        List<SpuExtPojo> spuExtPojoList = new ArrayList<>();
        for (ProductMangementPojo model : models) {
            SpuExtPojo spuExtPojo = new SpuExtPojo();
            Integer spuId = CreateNumberId.getUUIDInOrderId();
            spuExtPojo.setSpuId(spuId);
            for (Map.Entry<String, String> entry: model.getSpuProp().entrySet()) {
                spuExtPojo.setField(entry.getKey());
                spuExtPojo.setFieldValue(entry.getValue());
            }
            spuExtPojoList.add(spuExtPojo);
        }
        receiveDao.insertSpuExt(spuExtPojoList);
    }
}
